package com.haochang.chunkvod.model.custom;

import java.util.HashMap;

import org.json.JSONObject;

import com.haochang.base.BaseActivity;
import com.haochang.chunkvod.app.common.intent.IntentKey;
import com.haochang.chunkvod.app.config.ApiConfig;
import com.haochang.chunkvod.app.utils.ToastUtils;
import com.haochang.chunkvod.tools.thirdparty.network.http.httpenum.HttpMethodEnum;
import com.haochang.chunkvod.tools.thirdparty.network.http.httpenum.HttpRequestLoadingEnum;
import com.haochang.chunkvod.tools.thirdparty.network.http.request.HttpRequestBuilder;
import com.haochang.chunkvod.tools.thirdparty.network.http.request.HttpRequestTask;

import android.content.Intent;
import android.text.TextUtils;

/**
 *@author qintao
 *created at 2016/7/11 10:39
 * activty样式弹框数据请求
 */

public class CustomDialogData
{
	private static BaseActivity context;

	public CustomDialogData(BaseActivity context)
	{
		this.context = context;
	}

	public interface IRoomPeopleListener
	{
		void onRequestSuccess();
	}

	public interface ICancelGiveListener
	{
		void onRequestSuccess(JSONObject jsonObject);

		void onFail(int errno, String errMsg);
	}

	public interface IGoodsBackListener
	{
		void onRequestSuccess(JSONObject jsonObject);

		void onFail(int errno, String errMsg);
	}

	public interface IShiftListener
	{
		void onRequestSuccess(JSONObject jsonObject);
	}

	/**
	 * dialog修改房间备注
	 *
	 * @param bill_id
	 * @param remarks
	 */
	public void requestRemark(final String bill_id, final String remarks)
	{
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("bill_id", bill_id);
		map.put("remarks", remarks);
		HttpRequestTask httpRequestTask = new HttpRequestBuilder(context).interfaceName(ApiConfig.REMARK).param(map).httpMethodEnum(HttpMethodEnum.PUT)
				.httpRequestLoadingEnum(HttpRequestLoadingEnum.HTTP_LOADING_NONE).httpRequestSucessListener(new HttpRequestBuilder.IHttpRequestSucessListener()
				{

					@Override
					public void onResponseSucess(JSONObject jsonObject)
					{
						ToastUtils.makeText(context, "修改备注成功", ToastUtils.TOAST_TIME).show();
						Intent intent = new Intent();
						intent.putExtra(IntentKey.REMARK, remarks);
						intent.putExtra(IntentKey.BILL_ID, bill_id);
						context.setResult(context.RESULT_OK, intent);
						context.finish();
					}
				}).httpRequestFailedListener(new HttpRequestBuilder.IHttpRequestFailedListener()
				{

					@Override
					public void onError(int errno, String errMsg)
					{
						if (!TextUtils.isEmpty(errMsg))
							ToastUtils.makeText(context, errMsg, ToastUtils.TOAST_TIME).show();
						context.finish();
					}
				}).build();
		httpRequestTask.execute();
	}

	/**
	 * dialog修改房间顾客数
	 *
	 * @param billId
	 * @param content
	 */
	public void requestModifyRoomPeoPleNum(final String billId, final String content, final IRoomPeopleListener listener)
	{
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("bill_id", billId);
		map.put("customer_count", content);
		HttpRequestTask httpRequestTask = new HttpRequestBuilder(context).interfaceName(ApiConfig.AMOUNT).param(map).httpMethodEnum(HttpMethodEnum.PUT)
				.httpRequestLoadingEnum(HttpRequestLoadingEnum.HTTP_LOADING_DEFAULT)
				.httpRequestSucessListener(new HttpRequestBuilder.IHttpRequestSucessListener()
				{

					@Override
					public void onResponseSucess(JSONObject jsonObject)
					{
						ToastUtils.makeText(context, "修改成功", ToastUtils.TOAST_TIME).show();
						if (listener != null)
						{
							listener.onRequestSuccess();
						}
					}
				}).httpRequestFailedListener(new HttpRequestBuilder.IHttpRequestFailedListener()
				{

					@Override
					public void onError(int errno, String errMsg)
					{
						if (!TextUtils.isEmpty(errMsg))
							ToastUtils.makeText(context, errMsg, ToastUtils.TOAST_TIME).show();
					}
				}).build();
		httpRequestTask.execute();
	}

	/**
	 * dialog新增服务
	 *
	 * @param map
	 */
	public void requestAddService(HashMap<String, String> map)
	{
		HttpRequestTask httpRequestTask = new HttpRequestBuilder(context).interfaceName(ApiConfig.SERVICE).param(map).httpMethodEnum(HttpMethodEnum.POST)
				.httpRequestLoadingEnum(HttpRequestLoadingEnum.HTTP_LOADING_DEFAULT)
				.httpRequestSucessListener(new HttpRequestBuilder.IHttpRequestSucessListener()
				{

					@Override
					public void onResponseSucess(JSONObject jsonObject)
					{
						ToastUtils.makeText(context, "下单成功", ToastUtils.TOAST_TIME).show();
					}
				}).httpRequestFailedListener(new HttpRequestBuilder.IHttpRequestFailedListener()
				{

					@Override
					public void onError(int errno, String errMsg)
					{
						ToastUtils.makeText(context, "下单失败", ToastUtils.TOAST_TIME).show();
					}
				}).build();
		httpRequestTask.execute();
	}

	/**
	 * dialog取消增送
	 *@param isTakeOut 是否是外带
	 * @param map map参数
	 */
	public void requestCancelGive(boolean isTakeOut, HashMap<String, String> map, final ICancelGiveListener listener)
	{
		HttpRequestTask httpRequestTask = new HttpRequestBuilder(context).interfaceName(isTakeOut ? ApiConfig.TAKE_OUT_CANCEL_PRESENT : ApiConfig.PRESENT)
				.param(map).httpMethodEnum(HttpMethodEnum.DELETE).httpRequestLoadingEnum(HttpRequestLoadingEnum.HTTP_LOADING_NONE)
				.httpRequestSucessListener(new HttpRequestBuilder.IHttpRequestSucessListener()
				{

					@Override
					public void onResponseSucess(JSONObject jsonObject)
					{
						if (listener != null)
						{
							listener.onRequestSuccess(jsonObject);
						}
					}
				}).httpRequestFailedListener(new HttpRequestBuilder.IHttpRequestFailedListener()
				{

					@Override
					public void onError(int errno, String errMsg)
					{
						if (listener != null)
						{
							listener.onFail(errno, errMsg);
						}
					}
				}).build();
		httpRequestTask.execute();
	}

	public void requestCancelGive(HashMap<String, String> map, final ICancelGiveListener listener)
	{
		requestCancelGive(false, map, listener);
	}

	/**
	 * dialog退单
	 *@param  isTakeOut 是否是外带
	 * @param map
	 */
	public void requestBack(boolean isTakeOut, HashMap<String, String> map, final IGoodsBackListener listener)
	{
		HttpRequestTask httpRequestTask = new HttpRequestBuilder(context).interfaceName(isTakeOut ? ApiConfig.TAKE_OUT_RETREAT : ApiConfig.BACK).param(map)
				.httpMethodEnum(HttpMethodEnum.DELETE).httpRequestLoadingEnum(HttpRequestLoadingEnum.HTTP_LOADING_NONE)
				.httpRequestSucessListener(new HttpRequestBuilder.IHttpRequestSucessListener()
				{

					@Override
					public void onResponseSucess(JSONObject jsonObject)
					{
						if (listener != null)
						{
							listener.onRequestSuccess(jsonObject);
						}
					}
				}).httpRequestFailedListener(new HttpRequestBuilder.IHttpRequestFailedListener()
				{

					@Override
					public void onError(int errno, String errMsg)
					{

						if (listener != null)
						{
							listener.onFail(errno, errMsg);
						}
					}
				}).build();
		httpRequestTask.execute();
	}

	public void requestBack(HashMap<String, String> map, final IGoodsBackListener listener)
	{
		requestBack(false, map, listener);
	}

	/**
	 * dialog交班
	 */
	public void requestShift(final IShiftListener listener)
	{
		HttpRequestTask httpRequestTask = new HttpRequestBuilder(context).interfaceName(ApiConfig.SHIFT).httpMethodEnum(HttpMethodEnum.PUT)
				.httpRequestLoadingEnum(HttpRequestLoadingEnum.HTTP_LOADING_NONE).httpRequestSucessListener(new HttpRequestBuilder.IHttpRequestSucessListener()
				{

					@Override
					public void onResponseSucess(JSONObject jsonObject)
					{
						if (listener != null)
						{
							listener.onRequestSuccess(jsonObject);
						}
					}
				}).httpRequestFailedListener(new HttpRequestBuilder.IHttpRequestFailedListener()
				{

					@Override
					public void onError(int errno, String errMsg)
					{
						if (!TextUtils.isEmpty(errMsg))
							ToastUtils.showText(context, errMsg);
					}
				}).build();
		httpRequestTask.execute();
	}
}
